var ns, toast, UIActionProgress;
var app = new Vue({
    el: '#app',
    data: {
        companyname: '',
        username: '',
        bannerimg: '../image/header_bg.png',
        headimg: '../image/default.png',
        positionname: '',
        list: [],
        notice_msg: '',
        tongzhilist: [],
        form: {
            pageindex: 0,
            pagesize: 2,
        },
        companyname: '',
        current_item_length: 0,
        is_searching: false,
        can_scroll: false,
        isRead: true,
        notifyTimeOut: null,
        Status: '',
        is_scrolled: false,
        list: [{
            "id": 1,
            "name": "楼宇管家",
            "url": "chooseproject_frm",
            "css": "icon_sfgl",
            "count": 0,
            "type": 0,
            "fromtype": 4,
            "isshow": true
        }, {
            "id": 2,
            "name": "公表抄表",
            "url": "chooseproject_frm",
            "css": "icon_gbdj",
            "count": 0,
            "type": 2,
            "fromtype": 2,
            "isshow": true
        }, {
            "id": 3,
            "name": "总表抄表",
            "url": "chooseproject_frm",
            "css": "icon_zbdj",
            "count": 0,
            "type": 3,
            "fromtype": 3,
            "isshow": true
        }, {
            "id": 4,
            "name": "水电抄表",
            "url": "chooseproject_frm",
            "css": "icon_sbdj",
            "count": 0,
            "type": 0,
            "fromtype": 1,
            "isshow": true
        }, {
            "id": 5,
            "name": "设备巡检",
            "url": "equipment_frm",
            "css": "icon_sbxj",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 6,
            "name": "内部报事",
            "url": "neibubaoshi_add_frm",
            "css": "icon_nbbx",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 7,
            "name": "安防巡更",
            "url": "patrolplan_frm",
            "css": "icon_afxg",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 8,
            "name": "保洁绿化",
            "url": "bjlh_frm",
            "css": "icon_bjlh",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 9,
            "name": "门岗管理",
            "url": "gate_sentry_frm",
            "css": "icon_mggl",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 10,
            "name": "投诉建议",
            "url": "customerservicelist_frm",
            "css": "icon_tsjy",
            "count": 0,
            "type": 2,
            "isshow": true
        }, {
            "id": 11,
            "name": "品控管理",
            "url": "qualitycontrol_frm",
            "css": "icon_pkgl",
            "count": 0,
            "type": 0,
            "isshow": true
        }, {
            "id": 12,
            "name": "整改工单",
            "url": "qualitycontrollist_frm",
            "css": "icon_zggd",
            "count": 0,
            "type": 0,
            "isshow": true
        }],
        summary: {
            feeSummary: {},
            projectFeeList: [],
            serviceSummary: {}
        },
        scrollInterval: {
            projectInterval: null,
            msgInterval: null,
            is_scrolled: false
        },
        canViewFeeAnalysis: false,
        canViewServiceAnalysis: false,
    },
    methods: {
        openSetting: function() {
            ns.openWin('xitongshezhi_frm', '系统设置');
        },
        setStyle: function() {
            var that = this;
            var width = $api.offset($api.byId('cycle_container')).w;
            $api.css($api.byId('cycle_box'), 'width:' + width + 'px;height:' + width + 'px');
        },
        get_data: function() {
            var that = this;
            setTimeout(function() {
                that.get_gonggao();
            }, 1000);
            setTimeout(function() {
                that.get_data_count();
            }, 5000);
            setTimeout(function() {
                that.getAnalysisData();
            }, 2000);
            var options = {};
            options.action = "getnewmenulist";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.canViewFeeAnalysis = data.canViewFeeAnalysis;
                    that.canViewServiceAnalysis = data.canViewServiceAnalysis;
                    if (that.canViewServiceAnalysis) {
                        setTimeout(function() {
                            app.setStyle();
                        }, 200)
                    }
                    for (var i = 0; i < that.list.length; i++) {
                        var item = that.list[i];
                        for (var j = 0; j < data.list.length; j++) {
                            var item2 = data.list[j];
                            if (item2.id == item.id) {
                                item.isshow = item2.isshow;
                                break;
                            }
                        }
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_data_count: function() {
            var that = this;
            var options = {};
            options.action = "getmenucountlist";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    for (var i = 0; i < that.list.length; i++) {
                        if (that.list[i].name == '设备巡检') {
                            that.list[i].count = data.countform.DeviceTaskCount;
                        }
                        if (that.list[i].name == '安防巡更') {
                            that.list[i].count = data.countform.PatrolTaskCount;
                        }
                        if (that.list[i].name == '保洁绿化') {
                            that.list[i].count = data.countform.EventTaskCount;
                        }
                        if (that.list[i].name == '投诉建议') {
                            that.list[i].count = data.countform.SuggestionCount;
                        }
                        if (that.list[i].name == '整改工单') {
                            that.list[i].count = data.countform.QualityCount;
                        }
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1,
                servicetype: item.type,
                metertype: item.type,
                fromtype: item.fromtype || 0,
                selecttype: 2
            };
            ns.openWin(item.url, item.name, params);
        },
        open_tongzhixiaoxi: function() {
            ns.openWin('tongzhixiaoxi_frm', '通知公告');
        },
        open_gonggao_detail: function(id, title) {
            var that = this;
            ns.openWin('tongzhixiaoxidetail_frm', '通知公告', {
                id: id
            }, {
                needlogin: true
            });
            api.sendEvent({
                name: 'isreadtrue',
                extra: {
                    key1: 'value1'
                }
            });
        },
        get_gonggao: function() {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pageindex = that.form.pageindex;
            options.pagesize = that.form.pagesize;
            options.action = "getmsglist";
            ns.post(options, function(succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    that.tongzhilist = data.tongzhilist;
                    if (that.tongzhilist.length > 0) {
                        setTimeout(function() {
                            that.init_sroll('scrollmsgbox', 'scrollmsg');
                        }, 1000)
                    }
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        init_sroll: function(scrollElemID, leftElemID) {
            var that = this;
            var scrollOption = {};
            var scrollElem = $api.byId(scrollElemID);
            if (that.scrollInterval.is_scrolled) {
                var childNodes = scrollElem.childNodes;
                for (var i = 0; i < childNodes.length; i++) {
                    $api.remove(childNodes[i]);
                }
                if (scrollElemID == 'scrollmsgbox') {
                    if (that.scrollInterval.msgInterval) {
                        clearInterval(that.scrollInterval.msgInterval);
                        that.scrollInterval.msgInterval = null;
                    }
                }
                if (scrollElemID == 'scrollprojectbox') {
                    if (that.scrollInterval.projectInterval) {
                        clearInterval(that.scrollInterval.projectInterval);
                        that.scrollInterval.projectInterval = null;
                    }
                }
            }
            that.scrollInterval.is_scrolled = true;
            try {
                // scrollElem = document.getElementById("scrolllayer");
                with(scrollElem) {
                    style.width = 500; //宽度
                    noWrap = true;
                }
                scrollOption.preTop = 0;
                scrollOption.currentTop = 0;
                scrollOption.stoptime = 0;
                scrollOption.scrollCount = 0;
                // var leftElem = document.getElementById('scrollmessage');
                var leftElem = $api.byId(leftElemID);
                var childNodes = leftElem.childNodes;
                for (var i = 0; i < 2; i++) {
                    for (var j = 0; j < childNodes.length; j++) {
                        scrollElem.appendChild(childNodes[j].cloneNode(true));
                    }
                }

                // scrollElem.appendChild(leftElem.cloneNode(true));

                var elems = document.getElementsByName('notify_item');
                for (var i = 0; i < elems.length; i++) {
                    elems[i].onclick = function() {
                        var id = $api.attr(this, 'data-id');
                        if (scrollElemID == 'scrollmsgbox') {
                            that.open_gonggao_detail(id);
                        }
                    };
                }
            } catch (e) {}
            scrollElem.scrollTop = 0;
            if (scrollElemID == 'scrollmsgbox') {
                that.scrollInterval.msgInterval = setInterval(function() {
                    that.scroll_up(scrollElemID, leftElemID, scrollOption);
                }, 15); //滚动速度
            } else if (scrollElemID == 'scrollprojectbox') {
                that.scrollInterval.projectInterval = setInterval(function() {
                    that.scroll_up(scrollElemID, leftElemID, scrollOption);
                }, 15); //滚动速度
            }
        },
        scroll_up: function(scrollElemID, leftElemID, scrollOption) {
            var that = this;
            var scrollElem = $api.byId(scrollElemID);
            scrollOption.currentTop += 1;
            if (scrollOption.currentTop == 41) { //滚动距离
                scrollOption.stoptime += 1;
                scrollOption.currentTop -= 1;
                if (scrollOption.stoptime == 300) { //停顿时间
                    scrollOption.currentTop = 0;
                    scrollOption.stoptime = 0;
                    scrollOption.scrollCount++;
                    if (scrollOption.scrollCount >= 10) {
                        that.init_sroll(scrollElemID, leftElemID);
                    }
                }
            } else {
                scrollOption.preTop = scrollElem.scrollTop;
                scrollElem.scrollTop += 1;
                if (scrollOption.preTop == scrollElem.scrollTop) {
                    scrollElem.scrollTop = 0;
                    scrollElem.scrollTop += 1;
                }
            }
        },
        get_user_data: function() {
            var that = this;
            var companyname = api.getPrefs({
                sync: true,
                key: 'companyname'
            });
            that.companyname = companyname || '重庆永友网络科技有限公司';
            var username = api.getPrefs({
                sync: true,
                key: 'companyname'
            });
            var positionname = api.getPrefs({
                sync: true,
                key: 'positionname'
            });
            that.positionname = positionname || '没有岗位';
            that.username = username || '匿名用户';
            var headimg = api.getPrefs({
                sync: true,
                key: 'headimg'
            });
            that.headimg = headimg || '../image/default.png';
        },
        do_open_scan_login: function() {
            var that = this;
            ns.openFrame('scanner_frm', '扫码', {
                type: 0
            });
        },
        doOpenFeeSummary: function() {
            ns.openWin('analysis_historyfeesummary_frm', '收费统计');
        },
        doOpenFeeList: function(type) {
            ns.openWin('analysis_historyfeelist_frm', '收费明细', {
                type: type
            });
        },
        doOpenServiceSummary: function(type) {
            ns.openWin('analysis_customerservice_summary_frm', '工单统计');
        },
        doOpenServiceList: function(type) {
            ns.openWin('customerservicelist_frm', '工单明细', {
                isviewdetail: true
            });
        },
        getAnalysisData: function() {
            var that = this;
            if (!that.canViewFeeAnalysis && !that.canViewServiceAnalysis) {
                return;
            }
            var options = {
                action: 'gethomeanalysisdata'
            };
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.summary = data;
                    if (that.summary.projectFeeList && that.summary.projectFeeList.length > 0) {
                        setTimeout(function() {
                            that.init_sroll('scrollprojectbox', 'scrollprojectmsg');
                        }, 3000)
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    UIActionProgress = api.require('UIActionProgress');
    toast = new auiToast();
    setTimeout(function() {
        app.get_data();
    }, 500);
    setTimeout(function() {
        appupgrade.check_upgrade(true);
    }, 5000)
    ns.init_pull(true);
    app.get_user_data();
    api.addEventListener({
        name: 'changecentersuccess'
    }, function() {
        app.get_user_data();
    });
    api.addEventListener({
        name: 'isreadtrue'
    }, function(ret, err) {
        if (ret) {
            setTimeout(function() {
                app.get_data();
            }, 500);
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        }
    });
    api.addEventListener({
        name: 'onlogin'
    }, function(ret, err) {
        app.get_data();
    });
}
